package com.it.doudizhu3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import static java.lang.Integer.parseInt;

public class PokerGame {

    //牌盒
    static ArrayList<String> poker = new ArrayList<>();

    //创建一个集合，用来添加牌的价值
    static HashMap<String,Integer> hm = new HashMap<>();

    //准备牌
    static{
        String[] color = {"♦","♣","♥","♠"};
        String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};

        for(String c: color){
            for(String n: number){
                poker.add(c+n);
            }
        }
        poker.add(" 小王");
        poker.add(" 大王");

        //指定拍的价值
        //牌上的数字到Map集合中判断是否存在
        //存在，获取价值
        //不存在，本身就有价值
        hm.put("J",11);
        hm.put("Q",12);
        hm.put("K",13);
        hm.put("A",14);
        hm.put("2",15);
        hm.put("小王",50);
        hm.put("大王",100);
    }
    public PokerGame() {
        //洗牌
        Collections.shuffle(poker);
        //发牌
        //定义四个集合，底牌，地主牌，平民牌
        ArrayList<String> lord = new ArrayList<>();
        ArrayList<String> player1 = new ArrayList<>();
        ArrayList<String> player2 = new ArrayList<>();
        ArrayList<String> player3 = new ArrayList<>();
        //遍历集合，存储数据
        for(int i = 0; i < poker.size(); i++){
            if(i<=2){
                lord.add(poker.get(i));
                continue;
            }

            if(i%3 == 0){
                player1.add(poker.get(i));
            }else if(i%3 == 1){
                player2.add(poker.get(i));
            }else{
                player3.add(poker.get(i));
            }
        }

        order(lord);
        order(player1);
        order(player2);
        order(player3);

        //看牌
        lookPoker("底牌",lord);
        lookPoker("小周",player1);
        lookPoker("小肖",player2);
        lookPoker("小王",player3);
    }

    public void lookPoker(String name,ArrayList<String> list){
        System.out.print(name+":");
        for(String poker : list){
            System.out.print(poker+" ");
        }
        System.out.println();
    }
    //排序
    public void order(ArrayList<String> list){
        Collections.sort(list,new Comparator<String>() {
            //Array.sort(插入排序+二分查找）
            public int compare(String o1, String o2) {
                //01：表示当前要插入到有序列中的牌
                //02：表示已经在有序序列中存在的牌
                //负数：01小，插到前面
                //正数：02大，插到后面
                //0：01的数字跟02的数字一样，需要按照花色再次排序

                //1.计算o1的花色和价值
                String color1 = o1.substring(0,1);
                int value1 = getValue(o1);

                //2.计算o2的花色和价值
                String color2 = o2.substring(0,1);
                int value2 = getValue(o2);

                //3.比较o1和o2的价值
                int i = value1 - value2;
                return i == 0 ? color1.compareTo(color2) : i;
            }
        });
    }

    //计算牌的价值
    //参数：牌
    //返回值：价值
    public int getValue(String poker){
        String number = poker.substring(1);
        if(hm.containsKey(number)){
            //存在，获取价值
            return hm.get(number);
        }else{
            //不存在，类型转换
            return Integer.parseInt(number);
        }
    }
}
